package com.abou.mvc.controller.customer.product.search;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.abou.constant.product.EProductType;
import com.abou.model.common.product.search.SearchProductsRequest;
import com.abou.model.common.product.search.SearchProductsResult;
import com.abou.mvc.controller.ACommonController;
import com.abou.mvc.controller.ControllerUtils;

/**
 * The controller for the search page
 * 
 * @author khalid
 */

@Controller
@RequestMapping("/customer/products/search.do")
@SessionAttributes(ControllerUtils.SEARCH_REQUEST_ATT)
public class CustomerGlobalSearchController extends ACommonController {
  
  /**
   * The request mapping for home view
   */
  public static final String SEARCH_VIEW_REQUEST_MAPPING =
      "/customer/products/search.do";
  
  /**
   * The home view
   */
  public static final String SEARCH_VIEW = "customer/products/search";
  
  /**
   * The empty constructor
   */
  public CustomerGlobalSearchController() {

    super(SEARCH_VIEW, null);
  }
  
  /**
   * Shows the home page.
   * 
   * @param model The model
   * @return
   */
  @RequestMapping(method = RequestMethod.GET)
  @ModelAttribute(ControllerUtils.SEARCH_RESULT_ATT)
  public SearchProductsResult show(
      @ModelAttribute(ControllerUtils.SEARCH_REQUEST_ATT) SearchProductsRequest searchRequest) {

    return getProducts(searchRequest);
  }
  
  /**
   * Receives the form data and handles it.
   * 
   * @param searchRequest The search request data
   * @param result The binding result
   * @param request The HTTP request
   * @param rowsNbModif If not empty, the request modifies the rows number per
   *          page in the table
   * @return The search result
   */
  @RequestMapping(method = RequestMethod.POST)
  @ModelAttribute(ControllerUtils.SEARCH_RESULT_ATT)
  public SearchProductsResult submitForm(
      @ModelAttribute(ControllerUtils.SEARCH_REQUEST_ATT) SearchProductsRequest searchRequest) {

    return getProducts(searchRequest);
  }
  
  private SearchProductsResult getProducts(SearchProductsRequest searchRequest) {

    SearchProductsResult searchResult = null;
    
    EProductType productType = searchRequest.getProductType();
    String globalSearch = searchRequest.getGlobalSearch();
    
    if (searchRequest.getProductType() == null) {
      
      searchResult =
          getProductManager().listProductsByNameDescription(globalSearch,
              globalSearch, searchRequest.getSortOrder(),
              searchRequest.getPageIndex(), searchRequest.getRowsNbByPage());
      
    } else if (productType == EProductType.CLOTHING_BOY
        || productType == EProductType.CLOTHING_GIRL
        || productType == EProductType.CLOTHING_MAN
        || productType == EProductType.CLOTHING_WOMAN) {
      
      searchResult =
          getProductManager().listProductsClothes(globalSearch, globalSearch,
              productType, searchRequest.getClotheType(),
              searchRequest.getSortOrder(), searchRequest.getPageIndex(),
              searchRequest.getRowsNbByPage());
      
    } else {
      
      searchResult =
          getProductManager().listProducts(globalSearch, globalSearch,
              productType, searchRequest.getSortOrder(),
              searchRequest.getPageIndex(), searchRequest.getRowsNbByPage());
    }
    
    return searchResult;
  }
}
